package com.zhj.leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * 力扣 217 存在重复元素
 */
public class Test217 {
    public static void main(String[] args) {
        int[] nums1 = {1,2,3,4};
        int[] nums2 = {1,2,3,1};
        int[] nums3 = {1,2,3,1,4,2};
        System.out.println(isRepeat2(nums3));
    }

    private static boolean isRepeat(int[] nums) {
        Boolean flag = false;
        if (nums == null || nums.length == 0)
            return flag;
        Map<Integer,Integer> cache = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            if (cache.containsKey(nums[i])) {
                cache.put(nums[i],cache.get(nums[i]).intValue() + 1);
                flag = true;
            } else {
                cache.put(nums[i], 1);
            }
        }
        return flag;
    }

    private static boolean isRepeat2(int[] nums) {
        Boolean flag = false;
        if (nums == null || nums.length == 0)
            return flag;
        HashSet<Integer> hashSet = new HashSet();
        for (int i = 0; i < nums.length; i++) {
            hashSet.add(nums[i]);
        }
        return hashSet.size() != nums.length;
    }
}
